﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public static class SpritePrintUtils
{

    public static string PrintSpriteInfo(Sprite sp)
    {
        var sb = new StringBuilder();

        sb.Append($"===== {sp.name}\n");
        sb.Append($"pack: {sp.packed}, packMode:{sp.packingMode}, rot:{sp.packingRotation}");

        sb.Append($"texRect: {sp.textureRect.MyToString()}\n");
        sb.Append($"offset left_bottom: {sp.textureRectOffset.MyToString()}\n");
        var offsetRight = sp.rect.width - (sp.textureRectOffset.x + sp.textureRect.width);
        var offsetTop = sp.rect.height - (sp.textureRectOffset.y + sp.textureRect.height);
        sb.Append($"offset right_top: ({offsetRight}, {offsetTop})\n");
        sb.Append($"rect: {sp.rect.MyToString()}\n");
        sb.Append($"border: {sp.border.MyToString()}\n"); //left, bottom, right, top
        sb.Append($"bounds: {sp.bounds}");

        sb.Append($"padding: {UnityEngine.Sprites.DataUtility.GetPadding(sp).MyToString()}\n"); //比如:padding-left就是textureRect.x, padding-right就是rect.width - textureRect.width - textureRect.x

        var tex = sp.texture;

        var uv_o = UnityEngine.Sprites.DataUtility.GetOuterUV(sp);
        sb.Append($"uv_o: {uv_o.MyToString()}\n");
        sb.Append($"uv_o: {uv_o.x * tex.width}, {uv_o.y * tex.height}, {uv_o.z * tex.width}, {uv_o.w * tex.height}\n");

        var uv_i = UnityEngine.Sprites.DataUtility.GetInnerUV(sp);
        sb.Append($"uv_i: {uv_i.MyToString()}\n"); //比如: uv_i.x = uv_o.x - uv_offsetLeft, uv_i.y = uv_o.y - uv_offset_bottom
        sb.Append($"uv_i: {uv_i.x * tex.width}, {uv_i.y * tex.height}, {uv_i.z * tex.width}, {uv_i.w * tex.height}\n");

        sb.Append($"minSize: {UnityEngine.Sprites.DataUtility.GetMinSize(sp).MyToString()}\n"); //比如: x = border.x + border.z

        sb.Append($"=====vert\n");
        for (var i = 0; i < Image.s_VertScratch.Length; ++i)
        {
            var vert = Image.s_VertScratch[i];
            sb.Append($"{i}: {vert.MyToString()}\n");
        }

        sb.Append($"=====uv\n");
        for (var i = 0; i < Image.s_UVScratch.Length; ++i)
        {
            var uv = Image.s_UVScratch[i];
            sb.Append($"{i}: {uv.MyToString()}\n");
        }

        sb.Append($"=");

        return sb.ToString();
    }

    static string MyToString(this Vector2 vec2)
    {
        return $"({vec2.x}, {vec2.y})";
    }

    static string MyToString(this Vector3 vec3)
    {
        return $"({vec3.x}, {vec3.y}, {vec3.z})";
    }

    static string MyToString(this Vector4 vec4)
    {
        return $"({vec4.x}, {vec4.y}, {vec4.z}, {vec4.w})";
    }

    static string MyToString(this Rect rect)
    {
        return $"({rect.x}, {rect.y}, {rect.width}, {rect.height})";
    }


}
